﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using ExtraClub.Domain.Entities;
using System.Web.Security;
using ExtraClub.Domain;
using System.Net.Mail;
using LB.Generics;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Net;
using System.Net.Sockets;

namespace ExtraClub.Web.Forms.Service {
    /// <summary>
    /// Summary description for form
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class sign_up : System.Web.Services.WebService {

        [WebMethod(true)]
        public string HelloWorld() {
            return "EXTRA Club: Hellow World";
        }

        #region Create Demo Account
        /// <summary>
        /// Creation of Demo User Account
        /// 1. Creats a new user with pass = email
        /// 2. Marks it a not Approved
        /// 3. Sets global Expireaion Date (ED) relatd to Demo Account 
        /// 4. Creates a ticket = userID.Base64
        /// 5. Sends a message to the given email with ticket link
        /// 6. Response to the requester a message of success or failure
        /// </summary>
        /// <param name="nick"></param>
        /// <param name="email"></param>
        /// <returns></returns>
        [WebMethod(true)]
        public string CreateDemoAccount(string lang, string returnUrl, string user_name, string email) {
            string msg = "Success";
            HttpContext Context = HttpContext.Current;
            if (Context != null) {
                try {
                    if (!email.isEmail()) {
                        throw new MemberAccessException("Email not well-formed");
                    }
                    if (!user_name.isValidName()) {
                        throw new MemberAccessException("Name not well-formed");
                    }
                    MembershipUser user = Membership.CreateUser(user_name, email);
                    if (user != null) {
                        user.IsApproved = false;
                        user.Email = email;
                        Membership.UpdateUser(user);

                        AspnetMembership aspMembeship = AspnetMembership.FetchByID(user.ProviderUserKey.ToString());
                        int hours = int.Parse(ConfigurationManager.AppSettings["demo_expiratiion_term_in_hours"]);
                        aspMembeship.Expire = DateTime.Now.AddHours(hours);
                        aspMembeship.Save();

                        //string messageTemplate = WebUtils.FormUIText("new-account-created-message", Context.Request.AppRelativeCurrentExecutionFilePath.Replace("asmx", "xml"));

                        string confirmPageUrl = "http://some.com/create-demo-account";
                        string ticket = user.ProviderUserKey.EncodeTo64();
                        string url = confirmPageUrl + "?ticket=" + ticket;
                        string subject = "EXTRA Club";
                        //string message = string.Format(messageTemplate, url);

                        //LB.Generics.MailClient.Send(subject, message, new MailAddress("extra.clubs@gmail.com", "EXTRA", System.Text.Encoding.UTF8), email);
                        msg = "On the given email was sent a letter. To finish you regisration pleas click on the confirmation link";
                    }
                } catch (MemberAccessException ex) {
                    msg = ex.Message;
                }
            }
            return msg;
        }
        #endregion
    }
}
